package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.exception.UserNotLoginException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.ShoppingParamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public List<ShoppingCart> getShoppingCartList() {
        Long id = BaseContext.getCurrentId();
        log.info("查询购物车" + id.toString());
        return shoppingCartMapper.getShoppingCartList(id);
    }

    /*
     * 获取用户id,判断是否有此项
     * 有-数量加一
     * 没有 新增一条数据
     * */
    @Override
    public Integer addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //获取当前用户id
        Long currentId = BaseContext.getCurrentId();
        log.info("添加购物车:" + currentId.toString());
        if (currentId == null) {
            throw new UserNotLoginException("用户未登录");
        }

        Long dishId = shoppingCartDTO.getDishId();
        //List<ShoppingCart> shoppingCartList;
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentId)
                .dishId(dishId)
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .setmealId(shoppingCartDTO.getSetmealId())
                .build();
        //查询购物车中是否存在前端传过来的数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findShoppingCartByCondition(shoppingCart);
        //如果没有
        if (shoppingCartList == null || shoppingCartList.size() == 0) {
            //判断是单品还是套餐
            ShoppingParamVO shoppingParamVO;
            if (dishId != null) {
                shoppingParamVO = dishMapper.findShoppingById(dishId);
            } else {
                shoppingParamVO = setmealMapper.findShoppingById(shoppingCartDTO.getSetmealId());
            }
            //将内容封装并插入表中
            ShoppingCart cart = ShoppingCart.builder()
                    .userId(currentId)
                    .dishId(dishId)
                    .setmealId(shoppingCartDTO.getSetmealId())
                    .dishFlavor(shoppingCartDTO.getDishFlavor())
                    .name(shoppingParamVO.getName())
                    .amount(shoppingParamVO.getPrice())
                    .image(shoppingParamVO.getImage())
                    .number(1)
                    .createTime(LocalDateTime.now()).build();
            shoppingCartMapper.insertShoppingCart(cart);

        } else {
            //如果前端传来的数据已经存在,则数量+1
            ShoppingCart cart = shoppingCartList.get(0);
            shoppingCartMapper.updateNumber(cart.getId(), cart.getNumber() + 1);
        }
        return null;
    }

    @Override
    public Integer subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long currentId = BaseContext.getCurrentId();
        if (currentId == null) {
            throw new UserNotLoginException("用户未登录");
        }
        Long dishId = shoppingCartDTO.getDishId();
        //List<ShoppingCart> shoppingCartList;
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentId)
                .dishId(dishId)
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .setmealId(shoppingCartDTO.getSetmealId())
                .build();
        List<ShoppingCart> shoppingCartByCondition = shoppingCartMapper.findShoppingCartByCondition(shoppingCart);
        for (ShoppingCart cart : shoppingCartByCondition) {
            if (cart.getNumber() > 1) {
                shoppingCartMapper.deleteOne(shoppingCart);
            } else {
                shoppingCartMapper.deleteLast(shoppingCart);
            }
        }
        return null;
    }

    @Override
    public void deleteShoppingCart() {
        shoppingCartMapper.deleteShoppingCart( BaseContext.getCurrentId());
    }
}
